﻿using System;
using System.Threading;
using System.Diagnostics;

namespace ThreadJoin
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Console.WriteLine("Hello World!");
            //声明一个线程
            Thread thread = new Thread(new ThreadStart(Program.OutInfo));
            //启动线程
            thread.Start();
            Console.WriteLine("16 thread:"+Thread.CurrentThread.ManagedThreadId.ToString());
            Console.WriteLine("17 thread:"+thread.ManagedThreadId.ToString());

            ShowTime(thread, 100);
            Console.WriteLine("ShowTime is Over");
            Console.WriteLine("[Main] Over");
            stopwatch.Stop();
            Console.WriteLine("Application run time:"+stopwatch.Elapsed.TotalSeconds);
        }

        //输出数据
        public static void OutInfo()
        {
            Console.WriteLine("[Son] this is OutInfo");
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine($"[Son] this is {i}");
                Thread.Sleep(1000);
            }
            Console.WriteLine("[Son] Over");
            Console.WriteLine("32 thread:"+Thread.CurrentThread.ManagedThreadId.ToString());
        }

        public static void ShowTime(Thread thread,int time)
        {
            //在执行OutInfo方法的线程结束之前 一直运行
            while(!thread.Join(time)){
                Console.WriteLine($"41:{DateTime.Now}");
            }
        }
    }


}
